import JJCrypt from './JJCrypt';
import { JJUtil } from '.';
import { hexMD5 } from '../thirdparty';
import JJAPPConfig from '../resource/file/appConfig/JJAPPConfig';

export default class JJStorage {

    static JJStorageCacheIv = 'qwe345!@'
    static JJStorageCryptKey = 'rty567$%'

    static StorageWaySessionStorage = '01'
    static StorageWayLocalStorage = '02'

    /**
     * 本地存储
     * @method setItem
     * @for JJStorage
     * @param {String} key 需要存储的key
     * @param {any} val 需要存储的内容
     * @param {any} isCrypt 是否需要加密，默认JJAPPConfig.isStorageCrypt
     * @param {any} storageWay 存储方式 JJStorage.StorageWaySessionStorage 或 JJStorage.StorageWayLocalStorage
     * @return {boolean} true成功否则反之
     */
    static setItem = (key = String, val, isCrypt = JJAPPConfig.isStorageCrypt, storageWay = JJStorage.StorageWayLocalStorage) => {
        if (JJUtil.isNull(key) || JJUtil.isNull(val)) {
            return false
        }

        // 同一个域名区别不同平台
        key = JJAPPConfig.channelName + '_' + key

        val = { data: val }

        if (isCrypt) {
            key = hexMD5(key)
            val = JJCrypt.aesEncrypt(JSON.stringify(val), JJStorage.JJStorageCacheIv, JJStorage.JJStorageCryptKey);
        } else {
            val = JSON.stringify(val)
        }

        let result = true

        if (JJStorage.StorageWaySessionStorage === storageWay) {
            if (window.sessionStorage) {
                sessionStorage.setItem(key, val)
            } else {
                alert('浏览器不支持session存储，请更换浏览器再试！')
                result = false
            }
        } else if (JJStorage.StorageWayLocalStorage === storageWay) {
            if (window.localStorage) {
                localStorage.setItem(key, val)
            } else {
                alert('浏览器不支持local存储，请更换浏览器再试！')
                result = false
            }
        } else {
            alert('不识别的存储方式！')
            result = false
        }

        return result
    }

    /**
     * 读取本地存储
     * @method 方法名
     * @for JJStorage
     * @param {String} key 需要获取存储的key
     * @param {any} isCrypt 是否需要加密，默认JJAPPConfig.isStorageCrypt
     * @param {any} storageWay 存储方式 JJStorage.StorageWaySessionStorage 或 JJStorage.StorageWayLocalStorage
     * @return {any} 返回存储的值
     */
    static getItem = (key = String, isCrypt = JJAPPConfig.isStorageCrypt, storageWay = JJStorage.StorageWayLocalStorage) => {
        if (JJUtil.isNull(key)) {
            return null
        }

        // 同一个域名区别不同平台
        key = JJAPPConfig.channelName + '_' + key

        if (isCrypt) {
            key = hexMD5(key)
        }

        let getData = null

        if (JJStorage.StorageWaySessionStorage === storageWay && window.sessionStorage) {
            getData = sessionStorage.getItem(key)
        } else if (JJStorage.StorageWayLocalStorage === storageWay && window.localStorage) {
            getData = localStorage.getItem(key)
        } else {
            return null
        }

        if (JJUtil.isNotNull(getData)) {
            if (isCrypt) {
                getData = JJCrypt.aesDecrypt(getData, JJStorage.JJStorageCacheIv, JJStorage.JJStorageCryptKey)
            }

            getData = JSON.parse(getData)
            getData = getData.data
            return getData;
        } else {
            return null;
        }
    }

    /**
     * 删除本地存储
     * @method removeItem
     * @for JJStorage
     * @param {String} key 参数说明
     * @param {any} isCrypt 是否需要加密，默认=JJAPPConfig.isStorageCrypt
     * @param {any} storageWay 存储方式 JJStorage.StorageWaySessionStorage 或 JJStorage.StorageWayLocalStorage
     * @return {返回值类型} 返回值说明
     */
    static removeItem = (key = String, isCrypt = JJAPPConfig.isStorageCrypt, storageWay = JJStorage.StorageWayLocalStorage) => {
        if (JJUtil.isNull(key)) {
            return false
        }

        // 同一个域名区别不同平台
        key = JJAPPConfig.channelName + '_' + key
        
        if (isCrypt) {
            key = hexMD5(key)
        }

        let result = true

        if (JJStorage.StorageWaySessionStorage === storageWay && window.sessionStorage) {
            sessionStorage.removeItem(key)
        } else if (JJStorage.StorageWayLocalStorage === storageWay && window.localStorage) {
            localStorage.removeItem(key)
        } else {
            result = false
        }

        return result
    }

    /**
     * 清理本地存储
     * @method clear
     * @for JJStorage
     * @param {any} storageWay 存储方式 JJStorage.StorageWaySessionStorage 或 JJStorage.StorageWayLocalStorage
     * @return {返回值类型} 返回值说明
     */
    static clear = (storageWay = JJStorage.StorageWayLocalStorage) => {
        let result = true

        if (JJStorage.StorageWaySessionStorage === storageWay && window.sessionStorage) {
            sessionStorage.clear()
        } else if (JJStorage.StorageWayLocalStorage === storageWay && window.localStorage) {
            localStorage.clear()
        } else {
            result = false
        }

        return result
    }
}
